package org.lcx.pipeline.impl;

import java.util.HashMap;
import java.util.Iterator;

import org.lcx.pipeline.bean.PipelineResult;

/**
 * PipelineResult实现类
 *
 */
public class PipelineResultImpl implements PipelineResult {
	/**
	 * 存放错误信息map
	 */
	protected HashMap<Object, Object> mErrors = new HashMap<Object, Object>(7);

	public PipelineResultImpl() {
	}

	@Override
	public Object getError(Object pKey) {
		return this.mErrors.get(pKey);
	}

	@Override
	public void addError(Object pKey, Object pError) {
		this.mErrors.put(pKey, pError);
	}

	@Override
	public Object[] getErrorKeys() {
		Object[] keys = new Object[this.mErrors.size()];
		int i = 0;

		for (Iterator<Object> iter = this.mErrors.keySet().iterator(); iter.hasNext(); ++i) {
			keys[i] = iter.next();
		}

		return keys;
	}

	public Object[] getErrors() {
		Object[] errors = new Object[this.mErrors.size()];
		int i = 0;

		for (Iterator<Object> iter = this.mErrors.values().iterator(); iter.hasNext(); ++i) {
			errors[i] = iter.next();
		}

		return errors;
	}

	@Override
	public boolean hasErrors() {
		return this.mErrors.size() > 0;
	}

	@Override
	public void removeError(Object pKey) {
		this.mErrors.remove(pKey);
	}

	@Override
	public void copyInto(PipelineResult pResult) {
		Object[] keys = pResult.getErrorKeys();

		for (int i = 0; i < keys.length; ++i) {
			Object value = pResult.getError(keys[i]);
			this.addError(keys[i], value);
		}

	}
}
